package member.dao;

import member.model.Member;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

/**
 * <p>
 * This component and its source code representation are copyright protected and
 * proprietary to The Trivera Group, Inc., Worldwide D/B/A Trivera Technologies
 *
 * This component and source code may be used for instructional and evaluation
 * purposes only. No part of this component or its source code may be sold,
 * transferred, or publicly posted, nor may it be used in a commercial or
 * production environment, without the express written consent of the Trivera
 * Group, Inc.
 *
 * Copyright (c) 2015 The Trivera Group, LLC. http://www.triveratech.com
 * http://www.triveragroup.com
 * </p>
 * 
 * @author The Trivera Group Tech Team.
 */

public class MemberDAO {
	private static final Log log = LogFactory.getLog(MemberDAO.class);

	public Member getMember(String username)
	{
		Session session = SessionUtil.sessionFactory.getCurrentSession();
//		Transaction tx = session.beginTransaction();
		Query query = session
				//.createQuery("From Member m left join fetch m.addresses left join fetch m.reviews where m.username = :username");
				.createQuery("From Member m where m.username = :username");
		query.setString("username", username);
		Member member = (Member) query.uniqueResult();
//		tx.commit();
		return member;

	}
	
	public void addMember(Member member) {
		// todo complete this method
		// obtain the session and begin transaction
		Session session = SessionUtil.sessionFactory.getCurrentSession();
		Transaction xact = session.beginTransaction();
		session.save(member);
		xact.commit();
		// store the member
		// commit the transaction

	}

	public List getAllMembers() {
		Session session = SessionUtil.sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		@SuppressWarnings("unchecked")
		List<Member> result = session.createQuery("From Member").list();
		tx.commit();
		return result;
	}
}
